﻿using GN.Pay.TradeChannel.DefaultImpl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.YJPay.Response
{
    /// <summary>
    /// 付款通知实体
    /// </summary>
    [Serializable]
    class PayNotifyEntity : NotifyEntity
    {
        /// <summary>
        /// 银行代码
        /// </summary>
        public string bankCode { get; set; }

        /// <summary>
        /// 交易号
        /// </summary>
        public string tradeNo { get; set; }

        /// <summary>
        /// 交易付款金额 字符串  是  交易付款金额
        /// </summary>
        public decimal tradeAmount { get; set; }

        /// <summary>
        /// 收款人实收金额
        /// </summary>
        public decimal tradeAmountIn { get; set; }

        /// <summary>
        /// 业务状态
        /// </summary>
        public string fastPayStatus { get; set; }

        /// <summary>
        /// 平台手续费
        /// </summary>
        public decimal merchantChargeAmount { get; set; }

        /// <summary>
        /// 交易是否成功
        /// </summary>
        /// <returns></returns>

        public bool IsSuccess()
        {
            return this.success && "EXECUTE_SUCCESS".Equals(resultCode, StringComparison.CurrentCulture);
            //return
            //    "EXECUTE_SUCCESS".Equals(resultCode, StringComparison.CurrentCulture) &&
            //    "FINISHED".Equals(fastPayStatus, StringComparison.CurrentCulture);
        }


        public ITradeOrderQueryResult ToResult(IChannel channel)
        {
            if (string.IsNullOrWhiteSpace(this.orderNo))
            {
                channel.ThrowChannelValidationException("易极付通知商户订单号(orderNo)为空或者空白！");
            }
            if (string.IsNullOrWhiteSpace(this.merchOrderNo))
            {
                channel.ThrowChannelValidationException("易极付通知商户订单号(merchOrderNo)为空或者空白！");
            }
            if (string.IsNullOrWhiteSpace(this.tradeNo))
            {
                channel.ThrowChannelValidationException("易极付通知商户交易流水号(tradeNo)为空或者空白！");
            }
            if (this.tradeAmount <= 0)
            {
                channel.ThrowChannelValidationException("易极付交易金额小于或等于零！");
            }
            return new TradeOrderQueryResult(channel)
            {
                OrderNo = this.merchOrderNo,
                PayTime = this.notifyTime != null ? this.notifyTime.Value : DateTime.Now,
                TradeNo = this.tradeNo,
                Subject = "未知",
                Body = "未知",
                OrderMoney = this.tradeAmount,
                PayFee = 0m,
                RefundFee = 0m,
                RefundMoney = 0m,
                ExtraParam = context,
                BankType = bankCode                 
            };
        }
    }
}
